﻿using System;
using System.Collections.Generic;

namespace Daq.Tools.HttpClientHelper
{
    /// <summary>
    /// Get请求参数
    /// </summary>
    public class DaqGetRequest
    {
        /// <summary>
        /// 请求头
        /// </summary>
        public Dictionary<string, string> HeadersKv { get; private set; } = new Dictionary<string, string>();

        /// <summary>
        /// 请求的参数
        /// </summary>
        public Dictionary<string, string> ParamsKv { get; private set; } = new Dictionary<string, string>();

        /// <summary>
        /// 请求耗时时间秒
        /// </summary>
        public int TimeoutSecond { get; private set; } = 100;

        /// <summary>
        /// 添加请求超时前等待时间
        /// </summary>
        /// <param name="Timeout">秒，默认100秒</param>
        /// <returns></returns>
        public DaqGetRequest AddTimeout(int Timeout)
        {
            TimeoutSecond = Timeout;
            return this;
        }

        /// <summary>
        /// 添加请求头
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqGetRequest AddHeader(string name, string value)
        {
            HeadersKv.Add(name, value);
            return this;
        }

        /// <summary>
        /// 添加请求头
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqGetRequest AddHeader(Dictionary<string, string> headersKv)
        {
            if (headersKv.IsNotNullOrEmpty())
            {
                headersKv.ForEach(row => HeadersKv.Add(row.Key, row.Value));
            }
            return this;
        }

        /// <summary>
        /// 添加请求参数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DaqGetRequest AddParam(string name, string value)
        {
            ParamsKv.Add(name, value);
            return this;
        }

        /// <summary>
        /// 添加多个请求参数
        /// </summary>
        /// <param name="obj">可以是new {a = 1, b = 2}，也可以是Dictionary<string, string></param>
        /// <returns></returns>
        public DaqGetRequest AddParams(object obj)
        {
            var keyValues = obj.ToJson().ToObject<Dictionary<string, string>>();
            foreach (var kv in keyValues)
            {
                ParamsKv.Add(kv.Key, kv.Value);
            }
            return this;
        }
    }
}